package com.baili.controller.call;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baili.dto.BaseTransferEntity;
import com.baili.dto.ProductOrder;
import com.baili.service.call.CallService;
import com.baili.service.inspector.InspectorService;
import com.baili.util.MobileConfig;
import com.baili.util.StringUtil;

/**
 * @author iceX
 * @version 创建时间：2016年3月25日 上午9:59:58
 * @Name: CallController.java现场呼叫
 */
@Controller
@RequestMapping(value = "")
public class CallController {
	private static Logger log = Logger.getLogger(CallController.class);

	@Autowired
	private CallService callService;
	@Autowired
	private InspectorService inspectorService;

	/**
	 * 呼叫和主题表查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallTypeList")
	@ResponseBody
	public BaseTransferEntity getCallList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {

			String startTime = request.getParameter("startTime");

			String overTime = request.getParameter("overTime");

			ProductOrder productOrder = new ProductOrder();
			
//			if(StringUtil.isEmpty(startTime) && StringUtil.isEmpty(overTime)){
//				
//				startTime = sdf.format(StringUtil.MinusDay(new Date(), 7));
//				
//				overTime = sdf.format(new Date());
//			}
			
			productOrder.setStartTime(startTime);
			
			if(overTime != null && !overTime.equals("")){
				
				Date overTimeDate = StringUtil.parseDate2(overTime, "yyyy-MM-dd");
				
				Date overTimeDateAdd = StringUtil.addDay(overTimeDate, 1);
				
				productOrder.setOverTime(StringUtil.date2String(overTimeDateAdd));
				
			}

			// 获取列表数据
			List lst = callService.getCallTypeList(productOrder);

			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("getCallTypeList 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	@RequestMapping(value = "/service/getCallDealType")
	@ResponseBody
	public BaseTransferEntity getCallDealType(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int dealType = Integer.parseInt(request.getParameter("dealType"));
			// 获取列表数据
			List list = callService.getCallDealType(dealType);
			// 返回列表数据
			if (list == null || list.size() < 1) {

				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(list);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getCallDealType 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 呼叫是否被处理
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallYOrNHandle")
	@ResponseBody
	public BaseTransferEntity getCallYOrNHandle(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			// 获取列表数据
			
			String state = request.getParameter("state");

			ProductOrder productOrder = new ProductOrder();
			
			productOrder.setState(Integer.parseInt(state));
			
			List list = callService.getCallYOrNHandle(productOrder);
			// 返回列表数据
			if (list == null || list.size() < 1) {
				
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(list);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("getCallDealType 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}

	@RequestMapping(value = "/service/getCallDetails")
	@ResponseBody
	public BaseTransferEntity getCallDetails(HttpServletRequest request,

	HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			
			String id = request.getParameter("id");
			
			// 获取列表数据
			List<Map<String, Object>> list = callService.getCallDetails(Integer.parseInt(id));
			
			for(Map<String, Object> map:list){
				
				Date callTime = sdf.parse(map.get("callTime").toString());
				String waitingTime = "";
				long date1=callTime.getTime();
				long date2=(new Date()).getTime();

				long timeDifference = (date2-date1);
				
				long days = timeDifference/(24*60*60*1000);
				long hours = (timeDifference%(24*60*60*1000))/(60*60*1000);
				long minute = ((timeDifference%(24*60*60*1000))%(60*60*1000))/(60*1000);
				long second = (((timeDifference%(24*60*60*1000))%(60*60*1000))%(60*1000))/(1000);
				
				if(days>0){
					
					waitingTime = days+"天"+hours+"小时"+minute+"分钟";
				}else if(days<=0 && hours>0){
					
					waitingTime = hours+"小时"+minute+"分钟";
				
				}else if(days<=0 && hours<=0){
					
					waitingTime = minute+"分钟";
				}
				map.put("waitingTime", waitingTime);
				
				// 0表示图片浏览1表示视频浏览
				List<Map<String, Object>> pList = new ArrayList<Map<String,Object>>();
				
				pList = inspectorService.getCallOrDefectAndPictureOrVedio(Integer.parseInt(id),0,0);
				
				map.put("imageUrls", pList);
				
				List<Map<String, Object>> vList = new ArrayList<Map<String,Object>>();
				
				vList = inspectorService.getCallOrDefectAndPictureOrVedio(Integer.parseInt(id),1,0);
				
				map.put("videoUrls", vList);
				
			}
			
			// 返回列表数据
			if (list == null || list.size() < 1) {

				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(list);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getCallDetails 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 根据时间查询呼叫和主题表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getCallTypeListByTime")
	@ResponseBody
	public BaseTransferEntity getCallTypeListByTime(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String callTime = request.getParameter("callTime");
			String overTime = request.getParameter("overTime");
			// 获取列表数据
			List lst = callService.getCallTypeListByTime(callTime, overTime);

			int callTypeCount = callService.getCallTypeCount();
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			//baseTransferEntity.setTotalCount(Integer.toString(callTypeCount));
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getCallTypeList 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 是否处理
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/isHandleCall")
	@ResponseBody
	public BaseTransferEntity isHandleCall(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			int userId = Integer.parseInt(request.getParameter("userId"));
			// 获取列表数据
			callService.isHandleCall(id,sdf.format(new Date()),userId);

			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("getCallTypeList 呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 是否处理
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/deleteCallType")
	@ResponseBody
	public BaseTransferEntity deleteCallType(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		try {
			int callTypeId = Integer.parseInt(request.getParameter("callTypeId"));
			// 获取列表数据
			callService.deleteCallType(callTypeId);

			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));

		} catch (Exception e) {
			log.error("deleteCallType 删除呼叫主题--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

}
